package com.mlnx.stream.listener.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.SugarInfo;
import com.mlnx.mptp.push.PushPacket;
import com.mlnx.mptp.push.body.PushDataType;
import com.mlnx.push.MlnxPushClientService;
import com.mlnx.push.TopicFactory;
import com.mlnx.stream.listener.SugarListener;
import com.mlnx.tp.base.model.DeviceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by amanda.shan on 2019/11/1.
 */
@Component
public class SugarPushListener implements SugarListener {

    private MyLog log = MyLog.getLog(getClass());

    @Autowired
    private MlnxPushClientService mlnxPushService;

    @Autowired
    private TopicFactory topicFactory;


    @Override
    public void receiveSugarInfo(SugarInfo sugarInfo) {

        String topic = topicFactory.getSugarTopic(sugarInfo.getPatientId());

        Map<PushDataType, Object> pushDataMap = new HashMap<>();
        pushDataMap.put(PushDataType.SUGAR_INFO, sugarInfo);

        PushPacket packet = new PushPacket();
        packet.push(DeviceType.SERVER, topic, pushDataMap);

        mlnxPushService.push(topic, packet);

        log.debug("推送血糖数据:{}", sugarInfo);
    }

}
